package com.example.myapplication.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.myapplication.R;
import com.example.myapplication.activity.JinggaiActivity;
import com.example.myapplication.activity.LudengActivity;
import com.example.myapplication.activity.NongyeActivity;
import com.example.myapplication.activity.RentiActivity;
import com.example.myapplication.activity.SmokeActivity;
import com.example.myapplication.adapter.MyDeviceListAdapter;
import com.example.myapplication.bean.LotDevice;
import com.example.myapplication.common.IAMUserConfig;
import com.example.myapplication.common.http.MyHttp;
import com.example.myapplication.common.http.MyHttpCallback;
import com.example.myapplication.util.LoadingUtil;

import java.util.ArrayList;
import java.util.List;

public class DeviceFragment extends Fragment implements MyHttpCallback {

    private String product_id;
    private String product_name;
    private MyHttp myHttp;
    private SwipeRefreshLayout swipeRefreshLayout;
    //查询设备列表url
    private String url;

    public DeviceFragment() {

    }

    public DeviceFragment(String product_name, String product_id) {
        this.product_name = product_name;
        this.product_id = product_id;
    }

    private ListView listView;

    private List<LotDevice> lotDevices = new ArrayList<>();
    private MyDeviceListAdapter yanganAdapter;
    //视图
    private View view;
    private Dialog loadingDialog;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fg_yangan, container, false);
        initView();
        initData();
        return view;
    }

    private void initView() {
        //加载框
        loadingDialog = LoadingUtil.createLoadingDialog(getContext());
        loadingDialog.show();
        listView = view.findViewById(R.id.list_view);
        swipeRefreshLayout = view.findViewById(R.id.swipeLayout);
        //根据product_id,设定控制界面目标
        Class target = null;
        switch (product_name) {
            case "智慧烟感":
                //烟感
                target = SmokeActivity.class;
                break;
            case "智慧路灯":
                //路灯
                target = LudengActivity.class;
                break;
            case "智慧井盖":
                //井盖
                target = JinggaiActivity.class;
                break;
            case "智慧人体":
                //人体
                target = RentiActivity.class;
                break;
            case "智慧农业":
                //农业
                target = NongyeActivity.class;
                break;
        }
        yanganAdapter = new MyDeviceListAdapter(getContext(), lotDevices, target);
        listView.setAdapter(yanganAdapter);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //重新请求
                myHttp.queryHttpGet(url, 0);
            }
        });

    }

    private void initData() {
        myHttp = new MyHttp(getContext(), this);
        //listView.setAdapter(yanganAdapter);
        url = "https://iotda.cn-north-4.myhuaweicloud.com/v5/iot/" + IAMUserConfig.project_id + "/devices" +
                "?product_id=" + product_id;
        myHttp.queryHttpGet(url, 0);
    }


    @Override
    public void doMessage(Message message) {
        //获取设备列表
        if (message.what == 0) {
            loadingDialog.dismiss();
            swipeRefreshLayout.setRefreshing(false);
            String result = (String) message.obj;
            String devices = com.alibaba.fastjson.JSON.parseObject(result).getString("devices");
            List<LotDevice> list = com.alibaba.fastjson.JSON.parseArray(devices, LotDevice.class);
            yanganAdapter.setData(list);
        }
    }
}
